import {
  Base
} from '../../utils/base.js';
const {
  business,
  businesscollect
} = require('../../utils/api')
const {
  fetch,
  newFetch,
  hasSensitiveWords,
  replaceAll
} = require('../../utils/fetch')
var base = new Base();
const app = getApp()

//#endregion
// 

Page({
  data: {
    value: '',
    id: '',
    data: {},
    infodetails: '',
    imgs: [],
    reply_text: '',
    reply_list: [],
    is_collect: true,
    in_type: '',
    isEdit: true,
    browserList: [],
    specificarea: '100101',
  },
  onLoad: function (options) {
    if (wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea: wx.getStorageSync('specificarea')
      })
    }

    if (options.in_type == 'my') {
      this.setData({
        in_type: options.in_type
      })

      //获取用户商户
      this.initMerchant();

    } else {

      this.setData({
        in_type: '',
        id: options.id,
      })
      this.initData();
    }

    let userInfo = wx.getStorageSync('userInfo');

    newFetch(`wx/message/${this.data.specificarea}/saveBrowseLog`, 'GET', {
      messageId: this.data.id,
      memberId: userInfo["id"]
      // memberId: 140
    }).then(res => {})

    //  头像浏览列表
    newFetch(`wx/message/${this.data.specificarea}/getMemberList`, 'POST', {
      messageId: this.data.id
    }).then(res => {
      if (res) {
        this.setData({
          browserList: res
        })
      }
    })

  },
  initMerchant() {
    var that = this;
    let userInfo = wx.getStorageSync('userInfo');

    //商户详情 
    var param = {
      url: `store/${this.data.specificarea}/mylist`,
      data: {
        "memberId": userInfo["id"]
      },
      sCallback: function (res) {

        if (!res) {
          base.showmsg('您未发布您的商户');
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000);
        } else {
          var my = res.list[0];
          that.setData({
            id: my.id
          });
          that.initData();
        }
      }
    };
    base.request(param);
  },
  initData() {
    var that = this;
    fetch(`wx/store/${this.data.specificarea}/detail`, 'POST', {
      id: that.data.id
    }).then(res => {
      var details = res.details.split('&nbsp;').join('\n')
      this.setData({
        data: res,
        imgs: res.store_img.split(','),
        infodetails: details
      })
    })

    let userInfo = wx.getStorageSync('userInfo');
    //是否收藏
    if (userInfo) {
      var param = {
        url: `store/${this.data.specificarea}/iscollect`,
        data: {
          // "area": this.data.specificarea,
          "storeId": that.data.id,
          "memberId": userInfo["id"]
        },
        sCallback: function (data) {
          // that.setData({
          //   "is_collect": 1,
          // });
        }
      };
      base.request(param);
    }
    //评论列表
    this.get_reply_list();
  },

  get_reply_list() {
    var that = this;
    var param = {
      url: `reply/${this.data.specificarea}/list`,
      data: {
        // "area": this.data.specificarea,
        "type": 2,
        "typeid": this.data.id
      },
      sCallback: function (res) {
        if (!res) {
          res = {
            list: []
          }
        }
        that.setData({
          "reply_list": res.list,
        });
      }
    };
    base.request(param);
  },
  onChange: function (e) {
    this.data.reply_text = e.detail.value;
  },

  onConfirm() {
    var that = this
    if (this.data.reply_text == '') {
      base.showmsg('请输入评论');
      return;
    }
    wx.showLoading({
        title: '提交中...'
    })
    newFetch(`wx/reply/${this.data.specificarea}/fabu`, 'POST', {
      content: this.data.reply_text,
      memberid: wx.getStorageSync('userInfo').id,
      type: 2,
      typeid:that.data.id,
      area: that.data.specificarea,
    }).then(res => {
      base.showmsg('评论成功');
      wx.hideLoading()
      that.get_reply_list()

      this.setData({
        reply_text: ''
      })
    })
  },

  onClickCollect: function (e) {
    var that = this;
    if (!wx.getStorageSync('userInfo')) {
      wx.reLaunch({
        url: '/pages/login/index'
      })
      return;
    }
    let userInfo = wx.getStorageSync('userInfo');
    var param = {
      url: `store/${this.data.specificarea}/collect`,
      data: {
        'storeId': this.data.id,
        'memberId': userInfo["id"]
      },
      sCallback: function (data) {
        that.setData({
          is_collect: false
        });
        base.showmsg('已收藏');
      }
    };
    base.request(param);
  },
  scollect(event) {
    fetch(`wx/pay/${this.data.specificarea}/collectlist`, 'POST', {
      memberId: this.data.id,
    }).then(res => {
      this.setData({
        data: res,
        imgs: res.store_img.split(',')
      })
    })
  },
  tel: function (event) {
    wx.makePhoneCall({
      phoneNumber: this.data.data.store_tel,
    })
  },
  // 复制内容
  goinputCopy: function (e) {
    var details = e.currentTarget.dataset.details
    wx.setClipboardData({
      data: details,
      success: function (res) {
        // 添加下面的代码可以复写复制成功默认提示文本`内容已复制` 
        wx.showToast({
          title: '复制成功',
          duration: 1500
        })
        wx.getClipboardData({
          success: function (res) {}
        })
      }
    })
  },
  previewimg(options) {
    let {
      currentTarget
    } = options.currentTarget.dataset
    wx.previewImage({
      current: currentTarget,
      urls: this.data.imgs
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      //是否收藏    
      fetch(`wx/membershare/${this.data.specificarea}/addShare`, 'POST', {
        type: 2,
        typeId: that.data.id,
        memberId: userInfo["id"]
      }).then(res => {
        if (res) {}
      })
    }
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: this.data.data.store_name,
      path: '/pages/businessdetails/index/index?id=' + this.data.id,
      imageUrl: this.data.data.logo,
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
      complete: function (com) {}
    }
    // 返回shareObj
    return shareObj;
  },
  //用户点击右上角分享朋友圈
  onShareTimeline: function () {
    var that = this;
    var imglogo = this.data.data.logo || '/resources/imgs/agent.png'
    var imgUrl = this.data.data.logo.indexOf('http://www.xzwbfuwu.cn')
    if (imgUrl == 0) {
      var gologos = imglogo.replace('http://www.xzwbfuwu.cn', 'https://www.xzwbfuwu.cn')
      imglogo = gologos
    } else {
      imglogo = this.data.data.logo
    }

    return {
      title: this.data.data.store_name,
      path: '/pages/businessdetails/index/index?id=' + this.data.id,
      imageUrl: imglogo || '/resources/imgs/agent.png',
      query: {
        key: ''
      },
    }
  }

})